package es.ats.codington.festival.junit.services;

import static org.junit.Assert.*;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.*;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.junit.utils.ScriptRunner;
import es.ats.codington.festival.services.EventSignUpService;

public class TEventSignUpServices{

	private static EventSignUpService ess;
	private EventSignUp esu;
	private Event ev;
	
	
	
    @BeforeClass
    public static void onceExecutedBeforeAll() {
    	ess=new EventSignUpService();
		ScriptRunner sr=new ScriptRunner(ess.getEsd().getCon().connect(),true,true);
		try {
			FileReader fr=new FileReader("sql/newcodingtonCDTdb.sql");
			sr.runScript(fr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ess.getEsd().getCon().disconnect();
    }
    
	
	
	 @Before 
	       public void executedBeforeEach() {  
	      
		 
	        
			this.ess = new EventSignUpService();
			
			this.esu = new EventSignUp(2,3);
			
		
			Date d= new Date(1450992600);
			
			Place p=new Place(1,"Name of the place", 1000,"Description of the place",555, 0);
		
			this.ev=new Event("Name of the event", p, 5, "Description of the event",
					"Type of the event", 999.95F, 300, 1000, d, 0);
			       
	    }
	 
	 
	 
	 
	 
	 
		@Test
		   public void testIsRegistered() throws Exception{
			     
			 boolean aux;
			 
			 aux=ess.isRegisteredToEvent(esu);
		     assertEquals(true,aux);
		  
		  }
		
	 
		
		@Test
		   public void testviewEvents() throws Exception{
			
			 List<Event> events = new ArrayList<Event>();
			 events.add(ev);
			 
			 int a=esu.getIdVisitor();
			 
			 List<Event> list = ess.viewEventsOfUserDAO(a);
				 assertEquals(events,list);
					
	    }
	 
	
	
	@Test
	   public void testUpdateRestSeats() throws Exception{
		 
		  int aux; 
		  
		  aux=ess.updateRestSeatsAvailableEvent(ev);
			  assertEquals(ev.getSeatsAvailable(),aux);
		
	   }
		
	

	
	@Test
    public void testRegisterVisitor() throws Exception{
	  
	  int a=ess.registerVisitorForNewEventDAO(esu);
	  assertEquals(a, esu.getIdEvent());
	  
  }
	

		 
		 
	@Test
	   public void testUnregisterVisitor() throws Exception{
		
		 int a;
		 a=ess.unregisterVisitorForEventDAO(esu);
			  assertEquals(esu.getIdEvent(),a);
		
	}
	
	
	
	
	
}	

